// pages/business/addlogistic/addlogistic.js
var shihan = require('../../../common/fetch');
var config = require('../../../common/config');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    index: 0,
    shipment_no: '',
    shipment_list: '',
    shipment_array: ['请选择'],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) { //order_get_shipment_items  agent_add_logistic
    var url = '', title = '';
    this.setData({
      currentIndex: options.index
    })
    if (options.index == 3) {
      title = '进销统计';
    } else {
      this.getShipment();
      title = '填写物流信息';
      if (options.index == 1) {
        url = config.order_get_shipment_items;
      } else if (options.index == 2) {
        url = config.agent_return_add_logistic;
      }
      this.setData({
        ship_url: url,
        orderid: options.orderid
      })
    }
    wx.setNavigationBarTitle({
      title: title,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },
  getShipment: function() { //获取物流公司列表
    var that = this;
    shihan.sendRequest(config.order_get_shipment_items, 'POST', {})
      .then(function(response) {
        response.data.data.forEach(function(el, index) {
          that.setData({
            shipment_array: that.data.shipment_array.concat(el.sh_name)
          })
        })
        that.setData({
          shipment_list: response.data.data
        })
      }, function(error) {
        console.log(error);
      });
  },
  listenerPickerSelected: function(e) {
    var that = this
    if (that.data.selectflag) {
      that.setData({
        selectflag: false
      });
    } else {
      that.setData({
        selectflag: true
      });
    }
    that.setData({
      index: e.detail.value
    });
  },
  selectArrow: function() {
    if (this.data.selectflag) {
      this.setData({
        selectflag: false
      });
    } else {
      this.setData({
        selectflag: true
      });
    }
  },
  cancelSelect: function() {
    this.setData({
      selectflag: false
    });
  },
  shipmentInput: function(e) { //人工输入单号
    this.setData({
      shipment_no: e.detail.value
    });
  },
  sanCode: function() { //相机识别单号
    var that = this;
    wx.scanCode({ // 只允许从相机扫码
      onlyFromCamera: true,
      success: (res) => { //res.result == 箱码X-21de5432  盒码H-21de5432  
        that.setData({
          shipment_no: res.result
        });
      }
    })
  },
  formSubmit: function(e) {
    var that = this,
      data = {}, 
      formId = e.detail.formId;
    if (that.data.index == 0) {
      shihan.showToast.call(that, '请选择物流公司');
      return false;
    }
    if (!that.data.shipment_no) {
      shihan.showToast.call(that, '请填写物流单号');
      return false;
    }

    data['order_id'] = that.data.orderid;
    data['company'] = that.data.shipment_list[that.data.index - 1].sh_name;
    data['code'] = that.data.shipment_list[that.data.index - 1].sh_code;
    data['track_no'] = that.data.shipment_no;
    shihan.sendRequest(that.data.ship_url, 'POST', data)
      .then(function(response) {
        if (response.data.code == 0) {
          shihan.showToast.call(that, '提交成功');
          var pages = getCurrentPages();
          if (pages.length >= 2) {
            var prevPage = pages[pages.length - 2];
            prevPage.setData({
              isfresh: true
            })
          }
          setTimeout(function() {
            wx.navigateBack()
          }, 1000)
        } else {
          shihan.showToast.call(that, response.data.msg);
        }
      }, function(error) {
        console.log(error);
      });
    app.submissionForm(formId, '1', '5');
  },
  
  myagentInfo: function () {
    wx.navigateTo({
      url: '/pages/business/myagentinfo/myagentinfo?index=4&id=',
    })
  },
  agentInfo: function () {
    wx.navigateTo({
      url: '/pages/business/myagent/myagent?index=4',
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  }
})